#pragma  once
#include <unordered_map>
#include "base/core/singleton.h"
#include "base/core/object_pool.h"
#include "Common/MessageDispatch/IMessageDispatch.h"
#include "ProtoBuffer/Pay.pb.h"
#include "Common/TableData/RechargeRechargeCfg.h"

using namespace ProtoBuf;

class PayCenterMgr : public ManualSingleton<PayCenterMgr>,
					 public IServerMessageHandler,
					 public IClientMessageHandler
{
	typedef ::google::protobuf::RepeatedField< ::google::protobuf::uint32 > REPEATED_UINT32;
	typedef std::unordered_map<uint32_t, std::unordered_map<std::string, const RechargeRechargeCfgInfo*> > CHANN_PRODUCT_MAP;
	typedef std::unordered_map<std::string, const RechargeRechargeCfgInfo*> PRODUCT_MAP;
public:
	typedef bool(PayCenterMgr::*pFunc)(RECV_PACKAGE &package);
	typedef unordered_map<uint32_t, pFunc> mFunc;
	PayCenterMgr();
	virtual ~PayCenterMgr();

	bool				Init();
	bool				UnInit();
public:
	virtual void		onServerMessage(uint32_t actionId, RECV_PACKAGE &package);
	virtual const char* getServerHandlerDebugInfo(void) { return nullptr; };

	virtual void		onClientMessage(uint32_t actionId, RECV_PACKAGE &package, uint64_t Id = 0);
	virtual const char* getClientHandlerDebugInfo(void) { return "paycenterMgr"; };

	void                SetPayIndexs(uint32_t channId, REPEATED_UINT32 payIndex);
	void                SetPayOpen(uint32_t channId, bool bopen);
	void                UpdateOtherPayToLogic(uint32_t logicId = 0);

	void                RegisterWebPay();
	
protected:

	bool                CXyybReq(RECV_PACKAGE& package, uint64_t cid);
	bool                CXyybResultFromWeb(RECV_PACKAGE& package);

	bool                PayRspFromWeb(RECV_PACKAGE& package);

	bool                SendCurrencyToLogic(PayInfo* payInfo);
	bool                SendCurrencyToLogicFail(RECV_PACKAGE& package);

	bool                ClientPayReq(RECV_PACKAGE& package, uint64_t cid);
	bool                ClientPayRsp(const PayInfo& payInfo, const string& payRspString, uint32_t result);

	bool                ClientPayOrderReq(RECV_PACKAGE& package, uint64_t cid);
	bool                WebPayOrderRsp(RECV_PACKAGE& package);

	void                PayDebugLog(PayInfo* payInfo, string describe = "");


private:

	mFunc m_Func;
	const RechargeRechargeCfgInfo* GetRechargeCfg(uint32_t channId, string productId);
	PRODUCT_MAP				m_productCfgMap;
	CHANN_PRODUCT_MAP       m_channProductCfgMap;
	std::unordered_map<uint32_t, REPEATED_UINT32 > m_productIndexs;
	std::unordered_map<uint32_t, bool> m_channPayOpenMap;
};

#define g_MakePayCenterMgr() (PayCenterMgr::Instance())
#define g_GetPayCenterMgr()  (PayCenterMgr::GetInstance())
#define g_DelPayCenterMgr()  (PayCenterMgr::Destroy())
